package com.csiec.clock.fragment.main;

import androidx.databinding.DataBindingUtil;
import androidx.lifecycle.ViewModelProvider;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.csiec.clock.R;
import com.csiec.clock.activity.daka.JoinClockActivity;
import com.csiec.clock.activity.daka.MyClockActivity;
import com.csiec.clock.databinding.DakaFragmentBinding;
import com.csiec.clock.view.model.DakaViewModel;
import com.csiec.clock.view.model.MineViewModel;
import com.youth.banner.Banner;
import com.youth.banner.BannerConfig;
import com.youth.banner.Transformer;
import com.youth.banner.loader.ImageLoader;

import java.util.ArrayList;
import java.util.List;


public class DakaFragment extends Fragment {

    private DakaViewModel mViewModel;
    private DakaFragmentBinding binding;

    List<Integer> imageUrlData;
    List<String> contentData;
    private Banner myBanner;
    private Button myClock, joinClock;
    private final String TAG = DakaFragment.class.getSimpleName();

    public static DakaFragment newInstance() {
        return new DakaFragment();
    }

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        binding = DataBindingUtil.inflate(inflater, R.layout.daka_fragment, container, false);
        initBanner();
        return binding.getRoot();
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        mViewModel = new ViewModelProvider(this).get(DakaViewModel.class);
        mViewModel.init(requireActivity(),binding);
        binding.setDakaViewModel(mViewModel);
        binding.setLifecycleOwner(this);


        myClock = getActivity().findViewById(R.id.temperature_btn2);
        joinClock = getActivity().findViewById(R.id.temperature_btn1);

        //转到我的打卡界面
        binding.temperatureBtn2.setOnClickListener(v -> {
            Intent intent = new Intent(getActivity(), MyClockActivity.class);
            getActivity().startActivity(intent);
        });


        //转到加入的打卡界面
        binding.temperatureBtn1.setOnClickListener(v -> {
            Intent intent = new Intent(getActivity(), JoinClockActivity.class);
            getActivity().startActivity(intent);
        });

    }

    private void initBanner() {
        imageUrlData = new ArrayList<>();
        contentData = new ArrayList<>();
        imageUrlData.add(R.drawable.lunbo1);
        imageUrlData.add(R.drawable.lunbo2);
        contentData.add("积极防护，保护自己");
        contentData.add("戴口罩，勤洗手");
        binding.banner.setBannerStyle(BannerConfig.CIRCLE_INDICATOR_TITLE_INSIDE);
        binding.banner.setImageLoader(new MyLoader());
        binding.banner.setImages(imageUrlData);
        binding.banner.setBannerTitles(contentData);
        binding.banner.setBannerAnimation(Transformer.Default);
        //切换频率
        binding.banner.setDelayTime(4000);
        //自动启动
        binding.banner.isAutoPlay(true);
        //位置设置
        binding.banner.setIndicatorGravity(BannerConfig.CENTER);
        //开始运行
        binding.banner.start();
    }


    private class MyLoader extends ImageLoader {
        @Override
        public void displayImage(Context context, Object path, ImageView imageView) {
            Glide.with(getActivity()).load(path).into(imageView);
        }
    }

}